let arr=new Array();
$(function () {
    $("#tab").bootstrapTable({
        method:"post",
        contentType:"application/json",
        url:"emp",
        //设置交替样式
        striped:true,
        //当前页
        pageNumber:1,
        //是否显示分页
        pagination:true,
        //设置客户端还是服务端分页
        sidePagination: 'server',
        //一页多少条数据
        pageSize:4,
        pageList: [1,2,3],
        //上传到服务器的参数
        queryParams:function (pares) {
            return {
                //计算页号
                pageNum:(pares.offset/pares.limit)+1,
                //计算页大小
                pageSize:pares.limit,
                //查询条件
                name:$("#name").val(),
                sex:$("#sex").val(),
                startTime:$("#startTime").val(),
                endTime:$("#endTime").val(),
                dept:$("#dept").val()
            }
        },
        //定义列名
        columns:[
            {title:"编号",field:"employee.empno",align:'center'},
            {title:"姓名",field:"employee.ename",align:'center'},
            {title:"性别",field:"employee.job",align:'center'},
            {title:"日期",field:"employee.hiredate",align:'center'},
            {title:"部门",field:"dept.dname",align:'center'},
            {title:"操作",field:"employee.empno",formatter:function (value,rows,index) {
                    arr[index]=rows
                    var btn1="<button class=\"btn btn-primary\" style=\"margin:20px 0px;\" data-toggle='modal' data-target='#moTai' onclick='fff("+index+","+JSON.stringify(rows)+")' >修改</button>"
                    var btn2="<button class=\"btn btn-primary\" style=\"margin:20px 0px;\" data-toggle='modal' data-target='#moTai' onclick='fff(-2,"+value+")' >删除</button>"
                    return btn1+"&emsp;"+btn2
                }

            }
        ]
    })
    dept();
})
$("#query").click(function () {
    $("#tab").bootstrapTable("refresh")
})
//删除
$("#del").click(function () {
    $.ajax({
        url:"delEmp",
        type:"post",
        data:"id="+$("#modal_id").val(),
        success:function (data) {
            $("#tab").bootstrapTable("refresh")
        }
    })
})

// 修改和添加
$("#ok").click(function () {
    var emp={"empno":$("#modal_id").val(),
             "ename":$("#modal_name").val(),
             "job":$("#modal_sex").val(),
             "hiredate":$("#modal_date").val(),
             "deptno":$("#modal_dept").val()
            };
    $.ajax({
        url:"setAndAddEmp",
        type:"post",
        contentType:'application/json;charset=UTF-8',
        data:JSON.stringify(emp),
        success:function (data) {
            $("#tab").bootstrapTable("refresh")
        }
    })
})

//打开模态框
function  fff(index,value) {
    if(index==-1){
        $("#moTai h4").text("添加")
        $("#modal_id").val("1")
        $("#modal_name").val("")
        $("#modal_sex").val("")
        $("#modal_date").val("")
        $("#modal_dept").val("")
        $("#moTai .modal-body:eq(0)").show()
        $("#moTai .modal-body:eq(1)").hide()
    }else if(index==-2){
        $("#moTai h4").text("删除")
        $("#moTai .modal-body:eq(0)").hide()
        $("#moTai .modal-body:eq(1)").show()
        $("#modal_id").val(value)
    }
    else {
        $("#moTai h4").text("修改")
        let empAndDept =arr[index]
        $("#modal_id").val(empAndDept.employee.empno)
        $("#modal_name").val(empAndDept.employee.ename)
        $("#modal_sex").val(empAndDept.employee.job)
        $("#modal_date").val(empAndDept.employee.hiredate)
        $("#modal_dept").val(empAndDept.dept.deptno)
        $("#moTai .modal-body:eq(0)").show()
        $("#moTai .modal-body:eq(1)").hide()
    }
}
function dept() {
    $.ajax({
        url:"deptShowALl",
        type:"post",
        success:function (data) {
            $("#dept").html("<option value=\"0\">不限部门</option>");
            for(let i=0;i<data.length;i++)
                $("#dept").append($("<option value='"+data[i].deptno+"'>"+data[i].dname+"</option>"))
        }
    })
}

$(function ( ) {


})
